import { InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { SpawnLayoutConfig } from "GameLoop/WorldState/EntityManager/Entitys/room/spawn/spawnlayout";
import { PositionHelper } from "utils/position/PositionHelper";
import { StationNetwork } from "GameLoop/Path/StationNetwork";




/**
 * 填充角色,填充
 */ 
 /*@profile*/
export class Filler<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     * 搬运工行为,先做简化行为。运输的两端都有container
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
        const creep = arg.entity;
        const spawnentity = arg.entity.getEntity("SpawnEntity",creep.memory.OwnerRoom);
        // StationNetwork.getInstance.PathFinder.RegisterBlockCreep(creep);
        let workkey:string|false=spawnentity.getWorkKey(creep);
        if(!workkey)
        {
            workkey = spawnentity.registerCreep(creep);
            if(false === workkey)
            {
                // 注册失败
                creep.log("warn","找不到工作点")
                return []
            }
        }
        StationNetwork.getInstance.PathFinder.RegisterBlockCreep(creep);
        const workpos = SpawnLayoutConfig.parseVector(workkey);
        const abspos = spawnentity.getAbsulotePosition(workpos);
        // creep.log("warn","获取工作点",JSON.stringify(abspos))
        if(   creep.pos.distance(abspos)>0)
        {
            
            creep.mymoveTo({pos: abspos,range:0},{});
            return []
        }
        if(creep.room.controller!.level>=8 && creep.ticksToLive && creep.ticksToLive<900)
        {
            const spawn = spawnentity.getSpawn(workpos);
            if(spawn)
            {
                if(!spawn.spawning)
                {
                    spawn.renewCreep(creep);
                }
            }
            else 
            {
                spawnentity.log("error",JSON.stringify(workpos),'找不到对应的spawn');
            }
        }
        // 转移能量
        return [ ActionArgHelper.actionFill(creep,workpos)];
    }
}
